<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>物料列表</title>
    <link rel="stylesheet" href="../../assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../../css/style.css">
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        .submenu { display: none; }
        .menu-item.active > .submenu { display: block; }
        .menu-arrow { transition: transform 0.2s; }
        .menu-item.active > a > .menu-arrow { transform: rotate(90deg); }
        .table th { background-color: #f8f9fa; }
        .submenu li a.active { color: #28a745; font-weight: bold; transform: translateY(2px); display: inline-block; }
        .search-area { background: #f8f9fa; padding: 10px; border-radius: 8px; margin: 10px 0; width: 100%; }
        .search-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
        .search-item { flex: 0 0 auto; margin-right: 10px; margin-bottom: 5px; }
        .search-item .form-control, .search-item .form-select { width: 120px; height: 31px; font-size: 14px; }
        .search-item .btn { padding: 4px 8px; }
        .search-label { display: block; margin-bottom: 5px; font-size: 14px; color: #555; white-space: nowrap; }
        .table-responsive { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
        .custom-table { width: 100%; border-collapse: separate; border-spacing: 0; }
        .custom-table th { background: #f8f9fa; color: #495057; font-weight: 600; padding: 12px 15px; border-bottom: 2px solid #dee2e6; white-space: nowrap; }
        .custom-table td { padding: 12px 15px; border-bottom: 1px solid #eee; vertical-align: middle; }
        .action-buttons { display: flex; gap: 8px; }
        .action-buttons .btn {
            padding: 4px 10px;
            font-size: 13px;
            min-width: 60px;
            white-space: nowrap;
        }
        .page-title { font-size: 22px; font-weight: 600; margin-bottom: 20px; color: #333; display: flex; align-items: center; gap: 10px; }
        .page-title-icon { font-size: 24px; }
        .ms-auto { margin-left: auto; }
        .add-button { padding: 8px 16px; display: flex; align-items: center; gap: 5px; font-weight: 500; }
        .content-wrapper { padding: 20px; background: #fff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin: 20px; }
        .detail-item {
            margin-bottom: 15px;
        }
        .detail-label {
            font-weight: 600;
            color: #495057;
            margin-right: 10px;
        }
        .modal-lg {
            max-width: 800px;
        }
        /* iOS风格模态框样式 */
        .ios-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.4);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 1050;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .ios-modal.show {
            display: flex;
            opacity: 1;
        }

        .ios-modal-content {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 0;
            max-width: 400px;
            width: 90%;
            max-height: 80vh;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
            transform: scale(0.8);
            transition: transform 0.3s ease;
        }

        .ios-modal.show .ios-modal-content {
            transform: scale(1);
        }

        .ios-modal-header {
            padding: 20px 20px 10px;
            text-align: center;
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        }

        .ios-modal-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin: 0;
        }

        .ios-modal-body {
            padding: 20px;
            max-height: 60vh;
            overflow-y: auto;
        }

        .ios-detail-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 0;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
        }

        .ios-detail-item:last-child {
            border-bottom: none;
        }

        .ios-detail-label {
            font-weight: 500;
            color: #666;
            font-size: 14px;
        }

        .ios-detail-value {
            font-weight: 400;
            color: #333;
            font-size: 14px;
            text-align: right;
            max-width: 60%;
            word-wrap: break-word;
        }

        .ios-modal-footer {
            padding: 15px 20px 20px;
            text-align: center;
        }

        .ios-close-btn {
            background: #007AFF;
            color: white;
            border: none;
            border-radius: 12px;
            padding: 12px 30px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background 0.2s ease;
        }

        .ios-close-btn:hover {
            background: #0056CC;
        }

        /* 滚动条样式 */
        .ios-modal-body::-webkit-scrollbar {
            width: 4px;
        }

        .ios-modal-body::-webkit-scrollbar-track {
            background: transparent;
        }

        .ios-modal-body::-webkit-scrollbar-thumb {
            background: rgba(0, 0, 0, 0.2);
            border-radius: 2px;
        }
    </style>
</head>
<body>
<div class="admin-layout">
    <!-- 顶部导航栏 -->
    <header class="admin-header">
        <div class="header-left">
            <div class="logo">生产过程管理系统</div>
        </div>
        <div class="header-right">
            <span class="user-info">部门经理</span>
            <span class="user-name">
                    <script>
                        try {
                            const userInfo = localStorage.getItem('userInfo');
                            const user = JSON.parse(userInfo);
                            document.write(user.name || '未登录');
                        } catch (e) {
                            document.write('未登录');
                        }
                    </script>
                </span>
            <button class="logout-btn" onclick="logout()">退出</button>
        </div>
    </header>
    <!-- 左侧菜单 -->
    <aside class="admin-sidebar">
        <ul class="menu-list">
            <li class="menu-item">
                <a href="homepage/manager_home.html">
                    <span class="menu-icon">🏠</span>
                    <span class="menu-title">首页</span>
                </a>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">👥</span>
                    <span class="menu-title">人员管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../person/person_list.html">人员列表</a></li>
                    <li><a href="../person/person_edit.html">添加人员</a></li>
                    <li><a href="../person/schedule_edit.html">编辑排班表</a></li>
                    <li><a href="../person/schedule.html">查看排班表</a></li>
                    <li><a href="../person/schedule_statistics.html">班次统计</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">📦</span>
                    <span class="menu-title">物料管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../material/material_list.html">物料列表</a></li>
                    <li><a href="*">物料采购统计</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">⚙️</span>
                    <span class="menu-title">设备管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../device/device_list.html">设备列表</a></li>
                    <li><a href="*">设备报修报废统计</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">📝</span>
                    <span class="menu-title">产品管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../product/product_list.html" class="active">产品列表</a></li>
                    <li><a href="editproduct_plan.html">编辑生产计划</a></li>
                    <li><a href="viewproduct_plan.html">查看生产计划</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">🏢</span>
                    <span class="menu-title">供应商管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../provider/provider_list.html">供应商列表</a></li>
                </ul>
            </li>
        </ul>
    </aside>
    <!-- 主内容区域 -->
    <main class="admin-main">
        <div class="content-wrapper">
            <div class="content-header">
                <div class="content-header">
                    <div class="d-flex align-items-center justify-content-between mb-3">
                        <h2 class="product-title">产品列表</h2>
                    </div>
                    <div class="search-area">
                        <div class="search-row">
                            <div class="search-item">
                                <input type="text" class="form-control form-control-sm" id="searchCode" placeholder="产品编号">
                            </div>
                            <div class="search-item">
                                <input type="text" class="form-control form-control-sm" id="searchName" placeholder="产品名称">
                            </div>
                            <div class="search-item">
                                <input type="text" class="form-control form-control-sm" id="searchSize" placeholder="规格尺寸">
                            </div>
                            <div class="search-item">
                                <input type="text" class="form-control form-control-sm" id="searchBatch" placeholder="批次号">
                            </div>
                            <div class="search-item">
                                <input type="text" class="form-control form-control-sm" id="searchOperator" placeholder="加工人员">
                            </div>
                            <div class="search-item">
                                <input type="text" class="form-control form-control-sm" id="searchMethod" placeholder="加工方法">
                            </div>
                            <div class="search-item">
                                <select class="form-select form-select-sm" id="searchWorkshop">
                                    <option value="">选择车间</option>
                                    <option value="表面处理车间">表面处理车间</option>
                                    <option value="锻压车间">锻压车间</option>
                                    <option value="组装车间">组装车间</option>
                                    <option value="质检车间">质检车间</option>
                                </select>
                            </div>
                            <div class="search-item">
                                <button class="btn btn-sm btn-outline-primary" onclick="searchProducts()">🔍</button>
                            </div>
                            <div class="search-item">
                                <button class="btn btn-sm btn-outline-secondary" onclick="resetSearch()">↺</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="content-body">
                <div class="table-responsive">
                    <table class="table table-hover custom-table">
                        <thead>
                        <tr>
                            <th>产品编号</th>
                            <th>产品名称</th>
                            <th>规格尺寸</th>
                            <th>批次号</th>
                            <th>操作员</th>
                            <th>加工方法</th>
                            <th>加工时间</th>
                            <th>车间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="productTableBody">
                        <!-- 数据将通过JavaScript动态加载 -->
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>
</div>



<!-- 先引入 Bootstrap JS -->
<script src="../../assets/js/bootstrap.bundle.min.js"></script>
<!-- 再写你自己的 JS 代码 -->
<script>
    // 菜单展开/收起功能
    document.addEventListener('DOMContentLoaded', function () {
        var menuItems = document.querySelectorAll('.menu-item > a');
        menuItems.forEach(function (item) {
            item.addEventListener('click', function (e) {
                var parent = item.parentElement;
                // 只允许单开一个折叠区
                document.querySelectorAll('.menu-item').forEach(function (el) {
                    if (el !== parent) el.classList.remove('active');
                });
                parent.classList.toggle('active');
                e.preventDefault();
            });
        });

        // 添加首页链接的点击事件
        document.querySelector('.menu-item:first-child > a').addEventListener('click', function(e) {
            e.preventDefault(); // 阻止默认行为
            window.location.href = '../homepage/administrator_home.html'; // 跳转到管理员首页
        });

        // 默认展开产品管理菜单
        const productMenuItem = document.querySelector('.menu-item:nth-child(5)');
        if (productMenuItem) {
            productMenuItem.classList.add('active');
        }

        // 设置当前页面对应的菜单项为选中状态
        const currentPath = window.location.pathname;
        const filename = currentPath.substring(currentPath.lastIndexOf('/') + 1);
        document.querySelectorAll('.submenu li a').forEach(link => {
            if (link.getAttribute('href') === filename) {
                link.classList.add('active');
            }
        });

        // 加载产品数据
        loadProducts();
    });

    async function loadProducts() {
        try {
            const response = await fetch('http://localhost:8080/api/products');
            const data = await response.json();
            displayProducts(data);
        } catch (error) {
            console.error('加载产品数据失败:', error);
        }
    }

    // 搜索产品
    async function searchProducts(keyword) {
        try {
            const response = await fetch(`http://localhost:8080/api/products/search?keyword=${encodeURIComponent(keyword)}`);
            const data = await response.json();
            displayProducts(data);
        } catch (error) {
            console.error('搜索产品失败:', error);
        }
    }

    // 根据ID获取产品
    async function getProductById(id) {
        try {
            const response = await fetch(`http://localhost:8080/api/products/${id}`);
            const product = await response.json();
            return product;
        } catch (error) {
            console.error('获取产品详情失败:', error);
            return null;
        }
    }

    // 显示产品数据
    function displayProducts(products) {
        const tbody = document.getElementById('productTableBody');
        tbody.innerHTML = '';

        products.forEach(product => {
            const row = document.createElement('tr');
            row.innerHTML = `
                    <td>${product.productCode}</td>
                    <td>${product.name}</td>
                    <td>${product.size}</td>
                    <td>${product.batch}</td>
                    <td>${product.operatorName}</td>
                    <td>${product.processMethod}</td>
                    <td>${product.processTime}</td>
                    <td>${product.workshop}</td>
                    <td>
                        <button class="btn btn-sm btn-primary" onclick="viewProductDetails(${product.id})">查看详情</button>
                    </td>
                `;
            tbody.appendChild(row);
        });
    }

    // 退出登录函数
    function logout() {
        if (confirm('确定要退出登录吗？')) {
            localStorage.removeItem('userInfo');
            localStorage.removeItem('token');
            window.location.href = '../../../login.html';
        }
    }

    // 查看产品详情 - iOS风格模态框
    function viewProductDetails(id) {
        fetch(`http://localhost:8080/api/products/${id}`)
            .then(response => response.json())
            .then(product => {
                if (product) {
                    // 填充模态框数据
                    document.getElementById('detailProductCode').textContent = product.productCode || '未设置';
                    document.getElementById('detailProductName').textContent = product.name || '未设置';
                    document.getElementById('detailSize').textContent = product.size || '未设置';
                    document.getElementById('detailBatch').textContent = product.batch || '未设置';
                    document.getElementById('detailOperator').textContent = product.operatorName || '未设置';
                    document.getElementById('detailMethod').textContent = product.processMethod || '未设置';
                    document.getElementById('detailTime').textContent = product.processTime || '未设置';
                    document.getElementById('detailWorkshop').textContent = product.workshop || '未设置';

                    // 显示模态框
                    showProductDetailModal();
                }
            })
            .catch(error => {
                console.error('获取产品详情失败:', error);
                alert('获取产品详情失败，请稍后重试');
            });
    }

    // 显示iOS风格模态框
    function showProductDetailModal() {
        const modal = document.getElementById('productDetailModal');
        modal.classList.add('show');

        // 点击背景关闭模态框
        modal.onclick = function(e) {
            if (e.target === modal) {
                closeProductDetailModal();
            }
        };
    }

    // 关闭iOS风格模态框
    function closeProductDetailModal() {
        const modal = document.getElementById('productDetailModal');
        modal.classList.remove('show');
    }

    // ESC键关闭模态框
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            closeProductDetailModal();
        }
    });
</script>
</body>
</html>

<style>
    .content-wrapper {
        padding: 20px;
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        margin: 20px;
    }

    .content-header {
        display: flex;
        align-items: center;
        gap: 20px;
        padding: 10px;
    }

    .product-title {
        margin: 0;
        min-width: 80px;
        white-space: nowrap;
    }

    .search-area {
        flex: 1;
        margin: 0 10px;
    }

    .search-row {
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .search-item {
        flex: 0 0 auto;
    }

    .search-item .form-control,
    .search-item .form-select {
        width: 110px;
        height: 31px;
        font-size: 13px;
    }

    .search-item .btn {
        padding: 4px 8px;
        font-size: 14px;
    }
</style>

<script>
    // 菜单展开/收起功能
    document.addEventListener('DOMContentLoaded', function () {
        var menuItems = document.querySelectorAll('.menu-item > a');
        menuItems.forEach(function (item) {
            item.addEventListener('click', function (e) {
                var parent = item.parentElement;
                // 只允许单开一个折叠区
                document.querySelectorAll('.menu-item').forEach(function (el) {
                    if (el !== parent) el.classList.remove('active');
                });
                parent.classList.toggle('active');
                e.preventDefault();
            });
        });

        // 添加首页链接的点击事件
        document.querySelector('.menu-item:first-child > a').addEventListener('click', function(e) {
            e.preventDefault(); // 阻止默认行为
            window.location.href = '../homepage/administrator_home.html'; // 跳转到管理员首页
        });

        // 默认展开产品管理菜单
        const productMenuItem = document.querySelector('.menu-item:nth-child(5)');
        if (productMenuItem) {
            productMenuItem.classList.add('active');
        }

        // 设置当前页面对应的菜单项为选中状态
        const currentPath = window.location.pathname;
        const filename = currentPath.substring(currentPath.lastIndexOf('/') + 1);
        document.querySelectorAll('.submenu li a').forEach(link => {
            if (link.getAttribute('href') === filename) {
                link.classList.add('active');
            }
        });

        // 加载产品数据
        loadProducts();
    });

    async function loadProducts() {
        try {
            const response = await fetch('http://localhost:8080/api/products');
            const data = await response.json();
            displayProducts(data);
        } catch (error) {
            console.error('加载产品数据失败:', error);
        }
    }

    // 搜索产品
    function searchProducts() {
        const searchParams = {
            code: document.getElementById('searchCode').value.toLowerCase(),
            name: document.getElementById('searchName').value.toLowerCase(),
            size: document.getElementById('searchSize').value.toLowerCase(),
            batch: document.getElementById('searchBatch').value.toLowerCase(),
            operator: document.getElementById('searchOperator').value.toLowerCase(),
            method: document.getElementById('searchMethod').value.toLowerCase(),
            workshop: document.getElementById('searchWorkshop').value
        };

        const tbody = document.getElementById('productTableBody');
        const rows = tbody.getElementsByTagName('tr');

        for (let row of rows) {
            const cells = row.getElementsByTagName('td');
            const rowData = {
                code: cells[0].textContent.toLowerCase(),
                name: cells[1].textContent.toLowerCase(),
                size: cells[2].textContent.toLowerCase(),
                batch: cells[3].textContent.toLowerCase(),
                operator: cells[4].textContent.toLowerCase(),
                method: cells[5].textContent.toLowerCase(),
                workshop: cells[7].textContent
            };

            const matches = Object.keys(searchParams).every(key => {
                if (!searchParams[key]) return true; // 空值不参与筛选
                return rowData[key].includes(searchParams[key]);
            });

            row.style.display = matches ? '' : 'none';
        }
    }

    // 重置搜索条件
    function resetSearch() {
        const searchInputs = [
            'searchCode', 'searchName', 'searchSize', 'searchBatch',
            'searchOperator', 'searchMethod', 'searchWorkshop'
        ];

        searchInputs.forEach(id => {
            document.getElementById(id).value = '';
        });

        searchProducts(); // 重置后显示所有数据
    }

    // 为所有搜索输入框添加回车键搜索功能
    document.addEventListener('DOMContentLoaded', function() {
        const searchInputs = [
            'searchCode', 'searchName', 'searchSize', 'searchBatch',
            'searchOperator', 'searchMethod', 'searchWorkshop'
        ];

        searchInputs.forEach(id => {
            document.getElementById(id).addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    searchProducts();
                }
            });
        });
    });
</script>
</body>
</html>

<style>
    .content-wrapper {
        padding: 20px;
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        margin: 20px;
    }

    .content-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        padding-bottom: 15px;
        border-bottom: 1px solid #eee;
    }

    .content-header h2 {
        margin: 0;
        color: #333;
        font-size: 1.5rem;
        white-space: nowrap;
    }

    .search-area {
        background: #f8f9fa;
        padding: 10px;
        border-radius: 8px;
        margin: 10px 0;
        width: 100%;
    }

    .search-row {
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .search-item {
        flex: 0 0 auto;
    }

    .search-item .form-control,
    .search-item .form-select {
        width: 120px;
        height: 31px;
    }

    .search-item .btn {
        padding: 4px 8px;
        font-size: 14px;
    }

    @media (max-width: 768px) {
        .search-row {
            flex-wrap: wrap;
        }

        .search-item {
            flex: 1 1 auto;
        }
    }
</style>
<!-- iOS风格产品详情模态框 -->
<div class="ios-modal" id="productDetailModal">
    <div class="ios-modal-content">
        <div class="ios-modal-header">
            <h5 class="ios-modal-title">产品详情</h5>
        </div>
        <div class="ios-modal-body">
            <div class="ios-detail-item">
                <span class="ios-detail-label">产品编号</span>
                <span class="ios-detail-value" id="detailProductCode">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">产品名称</span>
                <span class="ios-detail-value" id="detailProductName">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">规格尺寸</span>
                <span class="ios-detail-value" id="detailSize">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">批次号</span>
                <span class="ios-detail-value" id="detailBatch">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">操作员</span>
                <span class="ios-detail-value" id="detailOperator">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">加工方法</span>
                <span class="ios-detail-value" id="detailMethod">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">加工时间</span>
                <span class="ios-detail-value" id="detailTime">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">车间</span>
                <span class="ios-detail-value" id="detailWorkshop">-</span>
            </div>
        </div>
        <div class="ios-modal-footer">
            <button class="ios-close-btn" onclick="closeProductDetailModal()">关闭</button>
        </div>
    </div>
</div>
<script>
    // 菜单展开/收起功能
    document.addEventListener('DOMContentLoaded', function () {
        var menuItems = document.querySelectorAll('.menu-item > a');
        menuItems.forEach(function (item) {
            item.addEventListener('click', function (e) {
                var parent = item.parentElement;
                // 只允许单开一个折叠区
                document.querySelectorAll('.menu-item').forEach(function (el) {
                    if (el !== parent) el.classList.remove('active');
                });
                parent.classList.toggle('active');
                e.preventDefault();
            });
        });

        // 添加首页链接的点击事件
        document.querySelector('.menu-item:first-child > a').addEventListener('click', function (e) {
            e.preventDefault(); // 阻止默认行为
            window.location.href = '../homepage/manager_home.html'; // 跳转到管理员首页
        });

        // 默认展开产品管理菜单
        const productMenuItem = document.querySelector('.menu-item:nth-child(5)');
        if (productMenuItem) {
            productMenuItem.classList.add('active');
        }

        // 设置当前页面对应的菜单项为选中状态
        const currentPath = window.location.pathname;
        const filename = currentPath.substring(currentPath.lastIndexOf('/') + 1);
        document.querySelectorAll('.submenu li a').forEach(link => {
            if (link.getAttribute('href') === filename) {
                link.classList.add('active');
            }
        });

        // 加载产品数据
        loadProducts();
    });

    async function loadProducts() {
        try {
            const response = await fetch('http://localhost:8080/api/products');
            const data = await response.json();
            displayProducts(data);
        } catch (error) {
            console.error('加载产品数据失败:', error);
        }
    }

    // 搜索产品
    function searchProducts() {
        const searchParams = {
            code: document.getElementById('searchCode').value.toLowerCase(),
            name: document.getElementById('searchName').value.toLowerCase(),
            size: document.getElementById('searchSize').value.toLowerCase(),
            batch: document.getElementById('searchBatch').value.toLowerCase(),
            operator: document.getElementById('searchOperator').value.toLowerCase(),
            method: document.getElementById('searchMethod').value.toLowerCase(),
            workshop: document.getElementById('searchWorkshop').value
        };

        const tbody = document.getElementById('productTableBody');
        const rows = tbody.getElementsByTagName('tr');

        for (let row of rows) {
            const cells = row.getElementsByTagName('td');
            const rowData = {
                code: cells[0].textContent.toLowerCase(),
                name: cells[1].textContent.toLowerCase(),
                size: cells[2].textContent.toLowerCase(),
                batch: cells[3].textContent.toLowerCase(),
                operator: cells[4].textContent.toLowerCase(),
                method: cells[5].textContent.toLowerCase(),
                workshop: cells[7].textContent
            };

            const matches = Object.keys(searchParams).every(key => {
                if (!searchParams[key]) return true; // 空值不参与筛选
                return rowData[key].includes(searchParams[key]);
            });

            row.style.display = matches ? '' : 'none';
        }
    }

    // 为所有搜索输入框添加回车键搜索功能
    document.addEventListener('DOMContentLoaded', function() {
        const searchInputs = [
            'searchCode', 'searchName', 'searchSize', 'searchBatch',
            'searchOperator', 'searchMethod', 'searchWorkshop'
        ];

        searchInputs.forEach(id => {
            document.getElementById(id).addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    searchProducts();
                }
            });
        });
    });
</script>
</body>
</html>

<style>
    .content-wrapper {
        padding: 20px;
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        margin: 20px;
    }

    .content-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        padding-bottom: 15px;
        border-bottom: 1px solid #eee;
    }

    .content-header h2 {
        margin: 0;
        color: #333;
        font-size: 1.5rem;
    }

    .header-actions {
        display: flex;
        gap: 10px;
    }

    .content-body {
        background: #fff;
        border-radius: 4px;
    }

    .table-responsive {
        overflow-x: auto;
        margin: 0 -20px;
        padding: 0 20px;
    }

    .custom-table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
    }

    .custom-table th {
        background: #f8f9fa;
        color: #495057;
        font-weight: 600;
        padding: 12px 15px;
        border-bottom: 2px solid #dee2e6;
        white-space: nowrap;
    }

    .custom-table td {
        padding: 12px 15px;
        border-bottom: 1px solid #eee;
        vertical-align: middle;
    }

    .custom-table tbody tr:hover {
        background-color: #f8f9fa;
    }

    .btn {
        padding: 8px 16px;
        border-radius: 4px;
        font-weight: 500;
    }

    .btn-sm {
        padding: 4px 8px;
        font-size: 0.875rem;
    }

    .btn-info {
        background-color: #17a2b8;
        border-color: #17a2b8;
        color: white;
        margin-right: 5px;
    }

    .btn-danger {
        background-color: #dc3545;
        border-color: #dc3545;
        color: white;
    }

    /* 响应式优化 */
    @media (max-width: 768px) {
        .content-wrapper {
            margin: 10px;
            padding: 15px;
        }

        .content-header {
            flex-direction: column;
            gap: 10px;
            align-items: flex-start;
        }

        .table-responsive {
            margin: 0 -15px;
            padding: 0 15px;
        }
    }
</style>